/// 用户数据模型
class User {
  final int? id;
  final String username;
  final String passwordHash;
  final String? realName;
  final String? email;
  final String? phone;
  final int role;
  final String? department;
  final String? position;
  final bool isActive;
  final DateTime? lastLoginTime;
  final DateTime createdAt;
  final DateTime updatedAt;
  final bool isDeleted;

  User({
    this.id,
    required this.username,
    required this.passwordHash,
    this.realName,
    this.email,
    this.phone,
    required this.role,
    this.department,
    this.position,
    required this.isActive,
    this.lastLoginTime,
    required this.createdAt,
    required this.updatedAt,
    required this.isDeleted,
  });

  /// 从Map创建User对象
  factory User.fromMap(Map<String, dynamic> map) {
    return User(
      id: map['id'] as int?,
      username: map['username'] as String,
      passwordHash: map['password_hash'] as String,
      realName: map['real_name'] as String?,
      email: map['email'] as String?,
      phone: map['phone'] as String?,
      role: map['role'] as int,
      department: map['department'] as String?,
      position: map['position'] as String?,
      isActive: (map['is_active'] as int) == 1,
      lastLoginTime: map['last_login_time'] != null 
          ? DateTime.parse(map['last_login_time'] as String) 
          : null,
      createdAt: DateTime.parse(map['created_at'] as String),
      updatedAt: DateTime.parse(map['updated_at'] as String),
      isDeleted: (map['is_deleted'] as int) == 1,
    );
  }

  /// 转换为Map
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'username': username,
      'password_hash': passwordHash,
      'real_name': realName,
      'email': email,
      'phone': phone,
      'role': role,
      'department': department,
      'position': position,
      'is_active': isActive ? 1 : 0,
      'last_login_time': lastLoginTime?.toIso8601String(),
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
      'is_deleted': isDeleted ? 1 : 0,
    };
  }

  /// 创建副本
  User copyWith({
    int? id,
    String? username,
    String? passwordHash,
    String? realName,
    String? email,
    String? phone,
    int? role,
    String? department,
    String? position,
    bool? isActive,
    DateTime? lastLoginTime,
    DateTime? createdAt,
    DateTime? updatedAt,
    bool? isDeleted,
  }) {
    return User(
      id: id ?? this.id,
      username: username ?? this.username,
      passwordHash: passwordHash ?? this.passwordHash,
      realName: realName ?? this.realName,
      email: email ?? this.email,
      phone: phone ?? this.phone,
      role: role ?? this.role,
      department: department ?? this.department,
      position: position ?? this.position,
      isActive: isActive ?? this.isActive,
      lastLoginTime: lastLoginTime ?? this.lastLoginTime,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      isDeleted: isDeleted ?? this.isDeleted,
    );
  }

  /// 检查是否为管理员
  bool get isAdmin => role == 2;

  /// 检查是否为普通用户
  bool get isUser => role == 1;

  /// 获取角色名称
  String get roleName {
    switch (role) {
      case 1:
        return '普通用户';
      case 2:
        return '管理员';
      default:
        return '未知角色';
    }
  }

  /// 获取显示名称
  String get displayName => realName ?? username;

  /// 验证用户数据
  static String? validateUsername(String? value) {
    if (value == null || value.isEmpty) {
      return '用户名不能为空';
    }
    if (value.length < 3) {
      return '用户名至少3个字符';
    }
    if (value.length > 50) {
      return '用户名不能超过50个字符';
    }
    if (!RegExp(r'^[a-zA-Z0-9_]+$').hasMatch(value)) {
      return '用户名只能包含字母、数字和下划线';
    }
    return null;
  }

  /// 验证密码
  static String? validatePassword(String? value) {
    if (value == null || value.isEmpty) {
      return '密码不能为空';
    }
    if (value.length < 6) {
      return '密码至少6个字符';
    }
    if (value.length > 20) {
      return '密码不能超过20个字符';
    }
    return null;
  }

  /// 验证真实姓名
  static String? validateRealName(String? value) {
    if (value == null || value.isEmpty) {
      return '真实姓名不能为空';
    }
    if (value.length < 2) {
      return '真实姓名至少2个字符';
    }
    if (value.length > 50) {
      return '真实姓名不能超过50个字符';
    }
    return null;
  }

  /// 验证邮箱
  static String? validateEmail(String? value) {
    if (value == null || value.isEmpty) {
      return null; // 邮箱可选
    }
    if (value.length > 100) {
      return '邮箱不能超过100个字符';
    }
    if (!RegExp(r'^[^@]+@[^@]+\.[^@]+$').hasMatch(value)) {
      return '邮箱格式不正确';
    }
    return null;
  }

  /// 验证手机号
  static String? validatePhone(String? value) {
    if (value == null || value.isEmpty) {
      return null; // 手机号可选
    }
    if (value.length != 11) {
      return '手机号必须是11位数字';
    }
    if (!RegExp(r'^\d+$').hasMatch(value)) {
      return '手机号只能包含数字';
    }
    return null;
  }

  /// 验证部门
  static String? validateDepartment(String? value) {
    if (value == null || value.isEmpty) {
      return null; // 部门可选
    }
    if (value.length > 100) {
      return '部门不能超过100个字符';
    }
    return null;
  }

  /// 验证职位
  static String? validatePosition(String? value) {
    if (value == null || value.isEmpty) {
      return null; // 职位可选
    }
    if (value.length > 100) {
      return '职位不能超过100个字符';
    }
    return null;
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is User &&
        other.id == id &&
        other.username == username;
  }

  @override
  int get hashCode {
    return id.hashCode ^ username.hashCode;
  }

  @override
  String toString() {
    return 'User(id: $id, username: $username, realName: $realName, role: $role, isActive: $isActive)';
  }
} 